angular.module('ted.mgt-controller', 
  [
  'ted.mgt-service',
  'ui.bootstrap',
  'angularFileUpload'
  ])
.controller('MgtMenuController', ['$scope', '$location', 
function($scope,$location) {

    var url = $location.absUrl();
    var menus = [
            {label:'新主题', url:"#!/mgt/newtopic",actived:true},
            {label:'主题管理', url:"#!/mgt/topics"},
            {label:'其他', url:"#!/mgt/statics"},
            {label:'回到首页', url:"#!/topic/hot"},
            ];
    for(var i=0,l=menus.length;i<l;i++){
        menus[i].actived = false;
        if(url.indexOf(menus[i].url) >= 0 ){
            menus[i].actived = true;
        }
    }
    $scope.menus = menus;
    $scope.clickmenu = function(idx){
        for(var i=0,l=menus.length;i<l;i++){
            menus[i].actived = false;
        }
        menus[idx].actived = true;
    };
}])
.controller('MgtNewTopicController', [
    '$scope','$http','$location','FileUploader','TedMgt', 
function($scope,$http,$location,FileUploader,TedMgt) {

  var uploader = $scope.uploader = new FileUploader({
    url: '/_api/mgt/fileupload'
  });  

  uploader.filters.push({
      name: 'customFilter',
      fn: function(item /*{File|FileLikeObject}*/, options) {
          return this.queue.length < 10;
      }
  });

  $scope.newtopic = {
    stime:'16:00'
  };

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();
    $scope.opened = true;
  };

  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };  
  $scope.format = 'yyyy-MM-dd';



  $scope.confirm = function (){
    var topic = $scope.newtopic;

    var _d = topic.sdate;
    var _h = topic.stime.split(':')[0];
    var _m = topic.stime.split(':')[1];
    topic.startdate = new Date(
      _d.getFullYear(),_d.getMonth(),_d.getDate(),
      _h,_m);

    topic.tags = topic.tagtext.split(',');


    if(!!uploader.queue[0]){
      uploader.queue[0].upload();
      uploader.onSuccessItem = function(fileItem, response, status, headers) {
        if(!!response.error){
          alert(response.error);
          uploader.queue.splice(0, 1);
        }else{
        topic.mkdoc = response.name;
          $http.post('/_api/mgt/topic',topic)
          .success(function(r, status, headers, config) {
              $location.url('/mgt/topics');
          })
          .error(function(data, status, headers, config) {
          });           
        }
      };    
    }else{
        $http.post('/_api/mgt/topic',topic)
        .success(function(r, status, headers, config) {
            $location.url('/mgt/topics');
        })
        .error(function(data, status, headers, config) {
        });       
    }

    
   


  };
}])
.controller('MgtTopicsController', [
    '$scope','$timeout','$routeParams','$http','$route','TedMgt', 
function($scope,$timeout,$routeParams,$http,$route,TedMgt) {
    TedMgt.getTopic('hot',function(ht){
      TedMgt.getTopics(function(data){
        $scope.topics = data;
        for(var i=0,l=$scope.topics.length;i<l;i++){
          
          if(!!$scope.topics[i].status && $scope.topics[i].status == 1){
            $scope.topics[i].banClassName = 'glyphicon-ban-circle';  
          }else{
            $scope.topics[i].banClassName = 'glyphicon-ok-circle';  
          }
          


          if(ht.tid == $scope.topics[i].tid){            
            $scope.topics[i].isHomePage = true;
            // break;
          }
        }
        
      });
    });

   


    $scope.toHomePage = function (topic){
      if(window.confirm('确定将《'+topic.title+'》推送到首页?')){
        $http.post('/_api/mgt/toHomePage',topic)
        .success(function(r, status, headers, config) {
          $scope.alerts = [
              { type: 'success', msg: '推送首页成功!' }
          ];
          $timeout(function(){
               $scope.alerts.splice(0, 1);
               $route.reload();
          }, 2000);

        })
        .error(function(data, status, headers, config) {
        }); 
      } 

    };

    $scope.ban = function (topic){
      var status = {status:1};
      var msg = '启用';
      if(!!topic.status && topic.status == 1){
          status = {status:0};
          msg = '停用';
      }

      if(window.confirm('确定'+msg+'《'+topic.title+'》?')){
        TedMgt.updateTopic(topic.tid, status, function(r){
          $route.reload();
        });
      }

    };

    $scope.remove = function (topic){
      if(window.confirm('确定删除《'+topic.title+'》?')){
        TedMgt.deleteByTid(topic.tid,function (d){
          $route.reload();
        });
      }

    };        
}])
.controller('MgtEnrollsController', [
    '$scope','$timeout','$routeParams','$http','$route','TedMgt', 
function($scope,$timeout,$routeParams,$http,$route,TedMgt) {
  var tid = $routeParams.tid;

  TedMgt.getTopic(tid,function(doc){
    $scope.topic = doc;
    TedMgt.getEnroll(tid, function(docs){
      $scope.enrolls = docs;
    });    
  });
    
}]);


